//
//  NewHomeStore.m
//  NewPro
//
//  Created by RYX on 2019/4/25.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "NewHomeStore.h"
#import "NewHomeModel.h"
#import "RecommendModel.h"

@implementation NewHomeStore

#pragma mark - 首页信息
- (void)getHomeInfoSuccess:(void(^)(NewHomeModel *model))success Failure:(void(^)(NSError *error))failure{
    
    NSString *url = [NSString stringWithFormat:@"%@/api/bannerinfo/homePage",IP];
    NSDictionary *dic = @{
                          @"customId":[UserDefaultsManager sharedInstance].user_id
                          };
    [HttpTool postUrlWithString:url parameters:dic success:^(id responseObject) {
        
        NSError *error = [HttpTool inspectError:responseObject];
        if (error == nil) {
            
            NewHomeModel *model = [NewHomeModel mj_objectWithKeyValues:responseObject[@"data"]];
            success(model);
        }else{
            
            failure(error);
        }
        
    } failure:^(NSError *error) {
        
        failure(error);
    }];
}

#pragma mark - 获取系统公告
- (void)getHomeToastSuccess:(void(^)(NSString *title, NSString *content, NSString *uid))success Failure:(void(^)(NSError *error))failure{
    
    NSString *url = [NSString stringWithFormat:@"%@/api/system/notice",IP];
    
    [HttpTool postUrlWithString:url parameters:nil success:^(id responseObject) {
        
        NSError *error = [HttpTool inspectError:responseObject];
        if (error == nil) {
            
            NSString *title = responseObject[@"data"][@"title"];
            NSString *str = responseObject[@"data"][@"content"];
            NSString *uid = responseObject[@"data"][@"id"];
            
            NSUserDefaults *de = [NSUserDefaults standardUserDefaults];
            
            if (![str isEqual:[NSNull null]]) {
                
                [de setObject:str forKey:@"systemContent"];
            }
            
            if (![uid isEqual:[NSNull null]]) {
                
                [de setObject:uid forKey:@"systemId"];
            }
    
            [de synchronize];
            
            if (str.length <= 0) {
                
                return failure(error);
            }
            
            
            success(title,str,uid);
            
        }else{
            
            failure(error);
        }
        
    } failure:^(NSError *error) {
        
        failure(error);
    }];
    
}

#pragma mark - 推荐列表
- (void)getRecommendListSuccess:(void(^)(NSArray *arr))success Failure:(void(^)(NSError *error))failure{
    
    
    NSString *url = [NSString stringWithFormat:@"%@/api/revision/list",IP];
    [HttpTool getUrlWithString:url parameters:nil success:^(id responseObject) {
        
        NSError *error = [HttpTool inspectError:responseObject];
        if (error == nil) {
            
            NSArray *listArr = [RecommendModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            success(listArr);
        }else{
            
            failure(error);
        }
        
    } failure:^(NSError *error) {
        
        failure(error);
    }];
    
}

/**
 推荐二级页面
 */
- (void)getRecommendTwoPageWithId:(NSString *)uid Success:(void(^)(NSArray *arr))success Failure:(void(^)(NSError *error))failure{
    
    NSString *url = [NSString stringWithFormat:@"%@/api/revision/list/%@",IP,uid];

    [HttpTool getUrlWithString:url parameters:nil success:^(id responseObject) {
        
        NSError *error = [HttpTool inspectError:responseObject];
        if (error == nil) {
            
            NSArray *listArr = [RecommendModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            success(listArr);
        }else{
            
            failure(error);
        }
        
    } failure:^(NSError *error) {
        
        failure(error);
    }];
    
}
@end
